package com.nlx.notes.module.controller;

import com.nlx.notes.module.bean.dto.accountbook.AccountBookDeleteDTO;
import com.nlx.notes.module.bean.dto.accountbook.AccountBookSaveDTO;
import com.nlx.notes.module.bean.dto.accountbook.AccountBookSwitchDTO;
import com.nlx.notes.module.bean.dto.accountbook.AccountBookUpdateDTO;
import com.nlx.notes.module.bean.vo.ResultVO;
import com.nlx.notes.module.bean.vo.accountbook.AccountBookVO;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.service.IAccountBooksService;
import com.nlx.notes.support.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 账本管理 前端控制器
 * </p>
 *
 * @author nlx
 * @since 2025-11-28
 */
@Slf4j
@RestController
@RequestMapping("/account-book")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class AccountBooksController extends BaseController {

    final IAccountBooksService accountBooksService;

    /**
     * 获取账本列表
     *
     * @return 账本列表
     */
    @GetMapping("/v1/list")
    public ResultVO<List<AccountBookVO>> list() {
        Long userId = UserHolder.getUserContext().getId();
        log.info("获取用户 {} 的账本列表", userId);
        try {
            List<AccountBookVO> list = accountBooksService.listByUser(userId);
            return ResultVO.success(list);
        } catch (Exception e) {
            log.error("获取账本列表失败", e);
            return ResultVO.error("获取账本列表失败: " + e.getMessage());
        }
    }

    /**
     * 创建账本
     *
     * @param saveDTO 保存DTO
     * @return 账本VO
     */
    @PostMapping("/v1/create")
    public ResultVO<AccountBookVO> create(@RequestBody AccountBookSaveDTO saveDTO) {
        Long userId = UserHolder.getUserContext().getId();
        log.info("用户 {} 创建账本，参数：{}", userId, JSONUtils.toString(saveDTO));
        try {
            AccountBookVO vo = accountBooksService.createBook(userId, saveDTO);
            return ResultVO.success(vo);
        } catch (IllegalArgumentException e) {
            log.error("创建账本失败：{}", e.getMessage());
            return ResultVO.error(e.getMessage());
        } catch (Exception e) {
            log.error("创建账本失败", e);
            return ResultVO.error("创建账本失败: " + e.getMessage());
        }
    }

    /**
     * 更新账本
     *
     * @param updateDTO 更新DTO
     * @return 账本VO
     */
    @PostMapping("/v1/update")
    public ResultVO<AccountBookVO> update(@RequestBody AccountBookUpdateDTO updateDTO) {
        Long userId = UserHolder.getUserContext().getId();
        log.info("用户 {} 更新账本，参数：{}", userId, JSONUtils.toString(updateDTO));
        try {
            AccountBookVO vo = accountBooksService.updateBook(userId, updateDTO);
            return ResultVO.success(vo);
        } catch (IllegalArgumentException e) {
            log.error("更新账本失败：{}", e.getMessage());
            return ResultVO.error(e.getMessage());
        } catch (Exception e) {
            log.error("更新账本失败", e);
            return ResultVO.error("更新账本失败: " + e.getMessage());
        }
    }

    /**
     * 删除账本
     *
     * @param deleteDTO 删除DTO
     * @return 是否成功
     */
    @PostMapping("/v1/delete")
    public ResultVO<Boolean> delete(@RequestBody AccountBookDeleteDTO deleteDTO) {
        Long userId = UserHolder.getUserContext().getId();
        log.info("用户 {} 删除账本，参数：{}", userId, JSONUtils.toString(deleteDTO));
        try {
            Boolean result = accountBooksService.deleteBook(userId, deleteDTO);
            return ResultVO.success(result);
        } catch (IllegalArgumentException e) {
            log.error("删除账本失败：{}", e.getMessage());
            return ResultVO.error(e.getMessage());
        } catch (Exception e) {
            log.error("删除账本失败", e);
            return ResultVO.error("删除账本失败: " + e.getMessage());
        }
    }

    /**
     * 切换账本
     *
     * @param switchDTO 切换DTO
     * @return 账本VO
     */
    @PostMapping("/v1/switch")
    public AccountBookVO switchBook(@RequestBody AccountBookSwitchDTO switchDTO) {
        Long userId = UserHolder.getUserContext().getId();
        log.info("用户 {} 切换账本，参数：{}", userId, JSONUtils.toString(switchDTO));
        return accountBooksService.switchBook(userId, switchDTO.getId());
    }

    /**
     * 获取当前账本
     *
     * @return 账本VO
     */
    @GetMapping("/v1/current")
    public AccountBookVO getCurrentBook() {
        Long userId = UserHolder.getUserContext().getId();
        log.info("获取用户 {} 的当前账本", userId);
        return accountBooksService.getCurrentBook(userId);
    }
}
